﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using shop.entity;
using shop.util;
using System.Data.SqlClient;
using System.Data;
namespace shop.dao
{
    public class OrderDao
    {
        public Order GetOrder(int id){
            string sql = "select o.id,userId, u.name as userName,goodsId, price, description,catatoryId, g.name as goodsName,PayStatus, o.orderTime from [order] o inner join [user] u on u.id = o.userId inner join goods g on g.id = o.goodsId and o.id = @id";
            SqlParameter[] pms = { 
                                 new SqlParameter("@id", id),
                              
                                 };
            DataTable dt = SqlHelper.ExecuteDataTable(sql, pms);
           
            Order order  = new Order();
            if (dt.Rows.Count>0) {
                EntityAdapt(order, dt.Rows[0]);
            }          
            return order;
        }
        public List<Order> ListOrder()
        {
       string sql = "select o.id,userId, u.name as userName,goodsId, description, price, catatoryId, g.name as goodsName, o.orderTime, PayStatus from [order] o inner join [user] u on u.id = o.userId inner join goods g on g.id = o.goodsId order by id desc";

            DataTable dt = SqlHelper.ExecuteDataTable(sql, null);
            List<Order> orderList = new List<Order>();
            for (int i = 0; i < dt.Rows.Count;i++ )
            {
                Order order = new Order();
                EntityAdapt(order, dt.Rows[i]);
                orderList.Add(order);
            }

            return orderList;
        }
          private void EntityAdapt(Order order,DataRow row){
            order.Id = (int)row["id"];          
            User user = new User() {
                Id = (int)row["userId"],
                Name = row["userName"].ToString()
        };
            Goods goods = new Goods() {
                Id = (int)row["goodsId"],
                Name = row["goodsName"].ToString(),
                CategoryId = (int)row["catatoryId"],
                Price = (double)row["price"],
                Description = row["description"].ToString()
            };
          
            order.OrderTime = row["orderTime"].ToString();
            order.PayStatus = (int)row["PayStatus"];
            order.User = user;
            order.Goods = goods;

        }
       
        public bool AddOrder(Order order) {
            string sql = "insert into [order](userId,goodsId,orderTime,PayStatus)  values(@userId,@goodsId,@orderTime,@PayStatus)";
            SqlParameter[] pms = { 
                                 new SqlParameter("@userId", order.UserId),
                                  new SqlParameter("@goodsId", order.GoodsId),
                                 new SqlParameter("@orderTime", order.OrderTime),
                                 new SqlParameter("@PayStatus", 1)
                                 };
            int count = SqlHelper.ExecuteNonQuery(sql,pms);
            if(count>0){
                return true;
            }
            return false;
        }

        public bool UpdateOrder(Order order)
        {
            string sql = "update [order] set userId=@userId,payStatus = @payStatus,goodsId = @goodsId  where id =@id";
            SqlParameter[] pms = { new SqlParameter("@id", order.Id),
                            new SqlParameter("@userId", order.UserId),
                            new SqlParameter("@payStatus", order.PayStatus),
                              new SqlParameter("@goodsId", order.GoodsId)
                                 };
            int count = SqlHelper.ExecuteNonQuery(sql, pms);
            if (count > 0)
            {
                return true;
            }
            return false;
        }

        public bool DeleteOrder(int id)
        {
            string sql = "delete from [order] where id = @id";
            SqlParameter[] pms = { new SqlParameter("@id", id) };
            int count = 0;          
             count = SqlHelper.ExecuteNonQuery(sql, pms);          
            if (count > 0)
            {
                return true;
            }
            return false;
        }
    }
}